package com.doctor.adapter;

import java.util.List;

import com.doctor.bean.DoctorBean;
import com.doctor.ui.R;
import com.doctor.ui.consulting.view.ConsultingActivity;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TechConsultingAdapter extends BaseAdapter{

	/**
	 * 技术咨询adapter
	 */
	
	private Context context;
	private TechConsultingAdapterCallback callback;
	private OnClick onClick;
	private OnLongClickTitle onLongClickTitle;
	private Integer currPosition=-1;
	private List<DoctorBean> beanList;
	
	public TechConsultingAdapter(Context context, List<DoctorBean> beanList, TechConsultingAdapterCallback callback){
		this.context=context;
		this.beanList=beanList;
		this.callback=callback;
		onClick=new OnClick();
		onLongClickTitle=new OnLongClickTitle();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beanList.size();
	}

	@Override
	public DoctorBean getItem(int arg0) {
		// TODO Auto-generated method stub
		return beanList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
	

	public Integer getCurrPosition() {
		return currPosition;
	}

	public void setCurrPosition(Integer currPosition) {
		this.currPosition = currPosition;
	}
	
	public void clear(){
		beanList.clear();
	}
	
	public boolean addBean(DoctorBean bean){
		boolean result= beanList.add(bean);
		return result;
	}
	
	public boolean removeBean(DoctorBean bean){
		boolean result=beanList.remove(bean);
		if (result) {
			notifyDataSetChanged();
		}
		return result;
	}
	

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (arg1==null) {
			holder=new ViewHolder();
			arg1=View.inflate(context, R.layout.consulting_list_item, null);
			holder.hideZone=(LinearLayout) arg1.findViewById(R.id.hideZone);
			holder.title=(LinearLayout) arg1.findViewById(R.id.title);
			holder.name=(TextView) arg1.findViewById(R.id.name);
			holder.hospital=(TextView) arg1.findViewById(R.id.hospital);
			holder.department=(TextView) arg1.findViewById(R.id.department);
			holder.postTitle=(TextView) arg1.findViewById(R.id.postTitle);
			holder.post=(TextView) arg1.findViewById(R.id.post);
			holder.advantage=(TextView) arg1.findViewById(R.id.advantage);
			holder.phoneBtn=(ImageView) arg1.findViewById(R.id.phoneBtn);
			holder.smsBtn=(ImageView) arg1.findViewById(R.id.smsBtn);
			holder.mailBtn=(ImageView) arg1.findViewById(R.id.mailBtn);
			holder.qqBtn=(ImageView) arg1.findViewById(R.id.qqBtn);
			holder.wxBtn=(ImageView) arg1.findViewById(R.id.wxBtn);
			holder.webSiteBtn=(ImageView) arg1.findViewById(R.id.websiteBtn);
			holder.detailBtn=(ImageView) arg1.findViewById(R.id.detailBtn);
			
			arg1.setTag(holder);
		} else {
			holder=(ViewHolder) arg1.getTag();
		}
		
		DoctorBean bean=getItem(arg0);
		
		holder.title.setTag(R.id.position_tag,arg0);
		holder.title.setOnClickListener(onClick);
		holder.title.setTag(R.id.bean_tag,bean);
		holder.title.setOnLongClickListener(onLongClickTitle);
		
		holder.phoneBtn.setTag(R.id.mobile_tag,bean.getMobile());
		holder.phoneBtn.setTag(R.id.phone_tag,bean.getTel());
		holder.phoneBtn.setOnClickListener(onClick);
		
		holder.smsBtn.setTag(R.id.sms_tag,bean.getMobile());
		holder.smsBtn.setOnClickListener(onClick);
		
		holder.mailBtn.setTag(R.id.mail_tag, bean.getEmail());
		holder.mailBtn.setOnClickListener(onClick);
		
		holder.qqBtn.setTag(R.id.qq_tag, bean.getQq());
		holder.qqBtn.setOnClickListener(onClick);
		
		holder.wxBtn.setOnClickListener(onClick);
		
		holder.webSiteBtn.setTag(R.id.url_tag, bean.getWebsite());
		holder.webSiteBtn.setOnClickListener(onClick);
		
		holder.detailBtn.setTag(R.id.bean_tag,bean);
		holder.detailBtn.setOnClickListener(onClick);
		
		//控制是否可见
		if (currPosition.intValue()==arg0) {
			if (holder.hideZone.getVisibility()==View.GONE) {
				holder.hideZone.setVisibility(View.VISIBLE);
			} else if (holder.hideZone.getVisibility()==View.VISIBLE) {
				holder.hideZone.setVisibility(View.GONE);
			}
		} else {
			holder.hideZone.setVisibility(View.GONE);
		}
		
		holder.name.setText(bean.getRealname());
		holder.hospital.setText(bean.getHospital());
		holder.department.setText(bean.getKs());
		holder.postTitle.setText(bean.getZc());
		holder.post.setText(bean.getZw());
		if (ConsultingActivity.map1.size()>0) {
			holder.advantage.setText(ConsultingActivity.map1.get(bean.getExpertise()));
		}		
		
		return arg1;
	}
	
	private class ViewHolder{
		LinearLayout title;
		LinearLayout hideZone;
		TextView name;
		TextView hospital;
		TextView department;
		TextView postTitle;
		TextView post;
		TextView advantage;
		ImageView phoneBtn;
		ImageView smsBtn;
		ImageView mailBtn;
		ImageView qqBtn;
		ImageView wxBtn;
		ImageView webSiteBtn;
		ImageView detailBtn;		
	}
	
	/**
	 * 点击按钮回调接口
	 */
	public interface TechConsultingAdapterCallback{
		public void onClickTitle(Integer position);
		public void onLongClickTitle(DoctorBean bean);
		public void onClickPhone(String mobileNumber, String phoneNumber);
		public void onClickSms(String mobileNumber);
		public void onClickEmail(String email);
		public void onClickQQ(String qq);
		public void onClickWx();
		public void onClickWebSite(String url);
		public void onClickDetail(DoctorBean bean);
	}
	
	/**
	 * 点击事件处理
	 * @author guo
	 */
	private class OnClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.title:
				callback.onClickTitle((Integer)v.getTag(R.id.position_tag));
				break;

			case R.id.phoneBtn:
				callback.onClickPhone((String) v.getTag(R.id.mobile_tag),(String) v.getTag(R.id.phone_tag));
				break;
				
			case R.id.smsBtn:
				callback.onClickSms((String) v.getTag(R.id.sms_tag));
				break;
				
			case R.id.mailBtn:
				callback.onClickEmail((String) v.getTag(R.id.mail_tag));
				break;	
				
			case R.id.qqBtn:
				callback.onClickQQ((String) v.getTag(R.id.qq_tag));
				break;
				
			case R.id.wxBtn:
				callback.onClickWx();
				break;
				
			case R.id.websiteBtn:
				callback.onClickWebSite((String) v.getTag(R.id.url_tag));
				break;	
				
			case R.id.detailBtn:
				callback.onClickDetail((DoctorBean) v.getTag(R.id.bean_tag));
				break;	
			}
		}		
	}
	
	private class OnLongClickTitle implements OnLongClickListener{
		@Override
		public boolean onLongClick(View v) {
			// TODO Auto-generated method stub
			callback.onLongClickTitle((DoctorBean) v.getTag(R.id.bean_tag));
			return false;
		}
		
	}
	
}
